#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'

from pymongo import MongoClient


# 连接MongoDB
def InitMongoDB(MONGODB_HOST, MONGODB_PORT, MONGODB_DB_NAME, MONGODB_COLLECTION_NAME):
    client = MongoClient(MONGODB_HOST, MONGODB_PORT)
    db = client[MONGODB_DB_NAME]
    collection = db[MONGODB_COLLECTION_NAME]
    return client, collection


# 关闭MongoDB
def CloseMongoDB(MongoDB_Connection):
    MongoDB_Connection.close()


def GetKeys():
    ht = {}
    client, collection = InitMongoDB("10.188.188.22", 27017, "eLMSG", "eLMSG_20180129")
    for result in collection.find():
        for key in result.keys():
            if key in ht:
                ht[key] += 1
            else:
                ht[key] = 1
    CloseMongoDB(client)

    for key in ht.keys():
        print(key + "\t" + str(ht[key]))


if __name__ == '__main__':
    GetKeys()
